package com.shipwe.loans.ui.fragment

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import com.shipwe.loans.R
import com.shipwe.loans.databinding.FragmentOrderPayPinFailedBinding
import com.shipwe.loans.pro.ShipweFragment
import com.shipwe.loans.ui.activity.ActivityOrderPayPin
import com.shipwe.loans.utils.event.AppEventPointType
import org.json.JSONObject

/**
 * shipwe
 * @Author wwdeng
 * @CreateTime: 2024/11/27 17:04
 * @Annotation：支付失败
 */
class FragmentOrderPayPinFailed(): ShipweFragment(){
    lateinit var binding: FragmentOrderPayPinFailedBinding
    private var orderData: JSONObject? = null
    private var act: ActivityOrderPayPin? = null

    constructor(act: ActivityOrderPayPin, data: JSONObject): this(){
        this.act = act
        orderData = data
    }

    override fun getShipweRootView(inflater: LayoutInflater): View {
        binding = FragmentOrderPayPinFailedBinding.inflate(inflater)
        return binding.root
    }

    override fun initShipweView() {
        rootViewBinding.rootLayout.setBackgroundResource(R.drawable.page_login_bg)
        setShipweTitleBarVisibility(View.VISIBLE)
        setShipweTextTitle(getString(R.string.loan_payment_status_title))
        setShipweRightBtnRes(R.drawable.customer_black)
    }

    override fun initShipweListener() {
        //客服
        rootViewBinding.rootCustomer.setOnClickListener {
            if (act != null){
                showCustomerDialog("PIN PaymentFailed Page",act!!.getOrderPayProductName(),act!!.getOrderPayProductToken())
            }
            appEventPointRequestAction(AppEventPointType.CLICK.SHIPWE_CLICK_CUSTOMER, "PIN PaymentFailed Page")
        }
        binding.orderPayFailThirdOrderIdCopy.setOnClickListener {
            if (orderData != null){
                onOrderPayFailCopyAction(orderData!!.optString("transactionId"))
            }
        }

        binding.orderPayFailPaymentAgain.setOnClickListener {
            appEventPointRequestAction(AppEventPointType.CLICK.SHIPWE_CLICK_LOAN_PAYMENT_FAILED_PAYMENT_AGAIN,"TransactionId：${orderData?.optString("transactionId")}")
            act!!.presenter!!.loadOrderPayLinkAction()
        }
    }

    override fun initShipweData() {
        refreshOrderPayFailData(orderData!!)
    }

    /**
     * 刷新数据
     */
    private fun refreshOrderPayFailData(data: JSONObject){
        orderData = data
        binding.orderPayFailAmount.text = data.optString("realRepayAmount","")
        binding.orderPayFailProductName.text = data.optString("product","")
        binding.orderPayFailThirdOrderId.text = data.optString("transactionId","")
        binding.orderPayFailedTime.text = data.optString("failedTime","")

        val failReason = data.optString("failReason","")
        if (failReason.isNotEmpty()){
            binding.orderPayFailReason.visibility = View.VISIBLE
            binding.orderPayFailReason.text = String.format(getString(R.string.loan_payment_status_failed_tips),failReason)
        } else {
            binding.orderPayFailReason.visibility = View.GONE
        }
    }

    /**
     * 复制
     */
    private fun onOrderPayFailCopyAction(orderId: String){
        appEventPointRequestAction(AppEventPointType.CLICK.SHIPWE_CLICK_LOAN_PAYMENT_FAILED_COPY, "TransactionId：$orderId")
        // 获取剪贴板管理器
        val clipboardManager = act?.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        // 创建一个 ClipData 对象，将要复制的文本放入其中
        val clipData = ClipData.newPlainText("label", orderId)
        // 将 ClipData 对象放入剪贴板
        clipboardManager.setPrimaryClip(clipData)
        showShipweToast(getString(R.string.customer_email_copy_success))
    }

    override fun onStart() {
        super.onStart()
        if (act != null){
            appEventPointRequestAction(AppEventPointType.PAGE.SHIPWE_PAGE_LOAN_PIN_REPAYMENT_FAILED_START,act!!.getOrderPayProductName())
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (act != null){
            appEventPointRequestAction(AppEventPointType.PAGE.SHIPWE_PAGE_LOAN_PIN_REPAYMENT_FAILED_END,act!!.getOrderPayProductName())
        }
    }
}